package com.ey12.db.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmOrder;
import com.ey12.db.domain.GmUser;
import com.ey12.utils.DateUtil;

/**
 * Data access object (DAO) for domain model class GmOrder.
 * 
 * @see com.ey12.db.domain.GmOrder
 * @author MyEclipse Persistence Tools
 */

public class GmOrderDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmOrderDAO.class);
	// property constants
	public static final String ORDER_NUM = "orderNum";
	public static final String BUY_NUM = "buyNum";
	public static final String PRICE = "price";
	public static final String NEED_MONEY = "needMoney";
	public static final String UNIT = "unit";
	public static final String TEL = "tel";
	public static final String QQ = "qq";
	public static final String CREATE_DATE = "createDate";
	public static final String PAY_STATE = "payState";
	public static final String PAY_DATE = "payDate";
	public static final String KF_UID = "kfUid";
	public static final String STATE = "state";
	public static final String FACT_MONEY = "factMoney";
	public static final String GAME_JUESE = "gameJuese";
	public static final String JUESE_LEVEL = "jueseLevel";
	public static final String BUY_WAY = "buyWay";
	public static final String REMARK = "remark";
	public static final String ORDERTYPE = "orderType";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmOrder transientInstance) {
		log.debug("saving GmOrder instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List findBuyNum1(GmUser guser) {
		try {
			String queryString = "select count(*) from GmOrder g where g.recState=1 and g.gmUser.userName='"
					+ guser.getUserName() + "'";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List findBuyNum2(GmUser guser) {
		try {
			String queryString = "select count(*) from GmOrder g where g.gmUser.userName='"
					+ guser.getUserName() + "'";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List findSaleNum1(GmUser guser) {
		try {
			String queryString = "select count(*) from GmOrder g where g.recState=1 and g.gmGameStore.gmUser.userName='"
					+ guser.getUserName() + "'";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List findSaleNum2(GmUser guser) {
		try {
			String queryString = "select count(*) from GmOrder g where g.gmGameStore.gmUser.userName='"
					+ guser.getUserName() + "'";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public void delete(GmOrder persistentInstance) {
		log.debug("deleting GmOrder instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmOrder findById(java.lang.Integer id) {
		log.debug("getting GmOrder instance with id: " + id);
		try {
			GmOrder instance = (GmOrder) getHibernateTemplate().get(
					"com.ey12.db.domain.GmOrder", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmOrder instance) {
		log.debug("finding GmOrder instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmOrder instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmOrder as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByOrderNum(Object orderNum) {
		return findByProperty(ORDER_NUM, orderNum);
	}

	public List findByBuyNum(Object buyNum) {
		return findByProperty(BUY_NUM, buyNum);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findByNeedMoney(Object needMoney) {
		return findByProperty(NEED_MONEY, needMoney);
	}

	public List findByUnit(Object unit) {
		return findByProperty(UNIT, unit);
	}

	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	public List findByQq(Object qq) {
		return findByProperty(QQ, qq);
	}

	public List findByCreateDate(Object createDate) {
		return findByProperty(CREATE_DATE, createDate);
	}

	public List findByPayState(Object payState) {
		return findByProperty(PAY_STATE, payState);
	}

	public List findByPayDate(Object payDate) {
		return findByProperty(PAY_DATE, payDate);
	}

	public List findByKfUid(Object kfUid) {
		return findByProperty(KF_UID, kfUid);
	}

	public List findByState(Object state) {
		return findByProperty(STATE, state);
	}

	public List findByFactMoney(Object factMoney) {
		return findByProperty(FACT_MONEY, factMoney);
	}

	public List findByGameJuese(Object gameJuese) {
		return findByProperty(GAME_JUESE, gameJuese);
	}

	public List findByJueseLevel(Object jueseLevel) {
		return findByProperty(JUESE_LEVEL, jueseLevel);
	}

	public List findByBuyWay(Object buyWay) {
		return findByProperty(BUY_WAY, buyWay);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findByOrderType(Object orderType) {
		return findByProperty(ORDERTYPE, orderType);
	}

	public List findAll() {
		log.debug("finding all GmOrder instances");
		try {
			String queryString = "from GmOrder order by createDate desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findMoneyBackDetial(GmUser user ) {
		try {
			String queryString = "from GmOrder g where g.gmUser.userName='"+user.getUserName()+"' and g.payState=2 order by g.createDate desc";
			System.out.println("findMoneyBackDetial="+queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findOrder(Integer state, String qq, String userName,
			String saler, String orderNum) {
		log.debug("finding all GmOrder instances");
		try {
			String queryString = "from GmOrder g where 1=1 ";
			if (state != null &&state != -1) {
				queryString += " and g.state='" + state + "'";
			}
			if (qq != null && qq != "") {
				queryString += " and g.qq='" + qq + "'";
			}
			if (userName != null && userName != "") {
				queryString += " and g.gmUser.userName='" + userName + "'";
			}
			if (saler != null && saler != "") {
				queryString += " and g.gmGameStore.gmUser.userName='" + saler
						+ "'";
			}
			if (orderNum != null && orderNum != "") {
				queryString += " and g.orderNum='" + orderNum + "'";
			}
			queryString = queryString + " order by g.createDate desc ";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List findHome() {
		try {
			String queryString = "from GmOrder g where g.recState=1 order by g.payDate desc";
			HibernateTemplate ht = getHibernateTemplate();
			// ht.setMaxResults(9);
			return ht.find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findSalerOrder(String username) {
		try {
			String queryString = "from GmOrder g where g.payState=1 and (g.gmGameStore.gmUser.userName='"
					+ username
					+ "' or g.operator='"
					+ username
					+ "') order by g.createDate desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findBuyerOrder(String username) {
		try {
			String queryString = "from GmOrder g where g.gmUser.userName='"
					+ username + "'  order by g.createDate desc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	// public List findGood(String userName){
	// try {
	// String queryString = "select orderNum,cserverName,unit,price,payState
	// from
	// GmOrder g where g.gmUser.userName=? order by payState desc ";
	// return getHibernateTemplate().find(queryString,userName);
	// } catch (RuntimeException re) {
	// log.error("find all failed", re);
	// throw re;
	// }
	// }

	public List findOrdersByGame(GmOrder porder) {
		try {
			String queryString = "from GmOrder o where 1=1";
			if (porder.getGmGame().getGameName() != null) {
				if ("".equals(porder.getGmGame().getGameName())) {
					queryString = queryString + " and o.gmGame.gameName="
							+ porder.getGmGame().getGameName();
				}
			}
			if (porder.getGmCserver().getCserverName() != null) {
				if ("".equals(porder.getGmCserver().getCserverName())) {
					queryString = queryString + " and o.gmCserver.cserverName="
							+ porder.getGmCserver().getCserverName();
				}
			}
			if (porder.getGmCserver().getSeverType() != null) {
				if ("".equals(porder.getGmCserver().getSeverType())) {
					queryString = queryString + " and o.gmCserver.severType="
							+ porder.getGmCserver().getSeverType();
				}
			}
			if (porder.getGmCserver().getUnit() != null) {
				if ("".equals(porder.getGmCserver().getUnit())) {
					queryString = queryString + " and o.gmCserver.unit="
							+ porder.getGmCserver().getUnit();
				}
			}
			queryString = queryString + " order by o.createDate desc ";
			System.out.println("findOrdersByGame--queryString=" + queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findFundDetial(GmOrder porder) {
		try {
			String queryString = "from GmOrder o where  o.state=1 and o.payState=1 ";
			if (porder.getCreateDate() != null && porder.getPayDate() != null) {
				if ((!"".equals(porder.getCreateDate()))
						&& (!"".equals(porder.getPayDate()))) {
					queryString = queryString + "and (o.createDate between '"
							+ DateUtil.formatDate(porder.getCreateDate())
							+ "' and '"
							+ DateUtil.formatDate(porder.getPayDate()) + "')";
				}
			}
			if (porder.getState() != null && porder.getState() != -1) {
				queryString = queryString + " and o.state=" + porder.getState();
			}
			if (porder.getOrderType() != null && porder.getOrderType() != -1) {
				queryString = queryString + " and o.orderType="
						+ porder.getOrderType();
			}
			if (porder.getGmUser() != null) {
				queryString = queryString + " and o.gmUser.id="
						+ porder.getGmUser().getId();
			}
			queryString = queryString + " order by o.createDate desc ";
			System.out.println("findFundDetial--queryString=" + queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findHistoryOrder(GmOrder porder) {
		try {
			String queryString = "from GmOrder o where o.payState=1";
			if (porder.getCreateDate() != null && porder.getPayDate() != null) {
				if ((!"".equals(porder.getCreateDate()))
						&& (!"".equals(porder.getPayDate()))) {
					queryString = queryString + " and (o.createDate between '"
							+ DateUtil.formatDate(porder.getCreateDate())
							+ "' and '"
							+ DateUtil.formatDate(porder.getPayDate()) + "')";
					System.out
							.println("findHistoryOrder-------------------------------date used");
				}
			}
			if (porder.getGmUser() != null) {
				queryString = queryString + " and o.gmUser.id="
						+ porder.getGmUser().getId();
			}
			if (porder.getGmCserver().getGmGame().getGameName() != null) {
				if (!"".equals(porder.getGmCserver().getGmGame().getGameName())) {
					queryString = queryString + " and o.gmCserver.gmGame.gameName='"
							+ porder.getGmCserver().getGmGame().getGameName() + "'";
				}
			}
			if (porder.getGmCserver().getCserverName() != null) {
				if (!"".equals(porder.getGmCserver().getCserverName())) {
					queryString = queryString
							+ " and o.gmCserver.cserverName='"
							+ porder.getGmCserver().getCserverName() + "'";
				}
			}
			if (porder.getGmCserver().getSeverType() != null) {
				if (!"".equals(porder.getGmCserver().getSeverType())) {
					queryString = queryString + " and o.gmCserver.severType like '%"
							+ porder.getGmCserver().getSeverType() + "%'";
				}
			}
			if (porder.getGmCserver().getUnit() != null) {
				if (!"".equals(porder.getGmCserver().getUnit())) {
					queryString = queryString + " and o.gmCserver.unit='"
							+ porder.getGmCserver().getUnit() + "'";
				}
			}
			queryString = queryString + " order by o.createDate desc ";
			List list = getHibernateTemplate().find(queryString);
			System.out.println("findHistoryOrder--queryString=" + queryString);
			return list;
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findOrderDetial(GmOrder porder) {
		try {
			String queryString = "from GmOrder o where 1=1";
			if (porder.getCreateDate() != null && porder.getPayDate() != null) {
				if ((!"".equals(porder.getCreateDate()))
						&& (!"".equals(porder.getPayDate()))) {
					queryString = queryString + " and (o.createDate between '"
							+ DateUtil.formatDate(porder.getCreateDate())
							+ "' and '"
							+ DateUtil.formatDate(porder.getPayDate()) + "')";
				}
			}
			if (porder.getState() != null && porder.getState() != -1) {
				queryString = queryString + " and o.state=" + porder.getState();
			}
			if (porder.getGmUser() != null) {
				queryString = queryString + " and o.gmUser.id="
						+ porder.getGmUser().getId();
			}
			if (porder.getGmGame().getGameName() != null) {
				if (!"".equals(porder.getGmGame().getGameName())) {
					queryString = queryString + " and o.gmGame.gameName='"
							+ porder.getGmGame().getGameName() + "'";
				}
			}
			if (porder.getGmCserver().getCserverName() != null) {
				if (!"".equals(porder.getGmCserver().getCserverName())) {
					queryString = queryString
							+ " and o.gmCserver.cserverName='"
							+ porder.getGmCserver().getCserverName() + "'";
				}
			}
			if (porder.getGmCserver().getSeverType() != null) {
				if (!"".equals(porder.getGmCserver().getSeverType())) {
					queryString = queryString + " and o.gmCserver.severType like '%"
							+ porder.getGmCserver().getSeverType() + "%'";
				}
			}
			if (porder.getGmCserver().getUnit() != null) {
				if (!"".equals(porder.getGmCserver().getUnit())) {
					queryString = queryString + " and o.gmCserver.unit='"
							+ porder.getGmCserver().getUnit() + "'";
				}
			}
			if (porder.getOrderNum()!= null) {
				if (!"".equals(porder.getOrderNum())) {
					queryString = queryString + " and o.orderNum='"
							+ porder.getOrderNum() + "'";
				}
			}
			queryString = queryString + " order by o.createDate desc ";
			System.out.println("findOrderDetial--queryString=" + queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findSalerOrderDetial(String username, GmOrder porder) {
		try {
			String queryString = "from GmOrder o where o.payState=1 ";
			if (porder.getOperator() != null) {
				if (!"".equals(porder.getOperator())) {
					queryString = queryString
							+ " and (o.gmGameStore.gmUser.userName='"
							+ porder.getOperator() + "' or o.operator='"
							+ porder.getOperator() + "') ";
				}
			} else {
				queryString = queryString + " and (o.gmGameStore.gmUser.userName='"
						+ username + "' or o.operator='" + username + "') ";
			}
			if (porder.getCreateDate() != null && porder.getPayDate() != null) {
				if ((!"".equals(porder.getCreateDate()))
						&& (!"".equals(porder.getPayDate()))) {
					queryString = queryString + " and (o.createDate between '"
							+ DateUtil.formatDate(porder.getCreateDate())
							+ "' and '"
							+ DateUtil.formatDate(porder.getPayDate()) + "')";
				}
			}
			if (porder.getState() != null) {
				if (porder.getState().intValue() != -1) {
					queryString = queryString + " and o.state="
							+ porder.getState();
				}
			}
			if (porder.getGmGame().getGameName() != null) {
				if (!"".equals(porder.getGmGame().getGameName())) {
					queryString = queryString + " and o.gmGame.gameName='"
							+ porder.getGmGame().getGameName() + "'";
				}
			}
			if (porder.getGmCserver().getCserverName() != null) {
				if (!"".equals(porder.getGmCserver().getCserverName())) {
					queryString = queryString
							+ " and o.gmCserver.cserverName='"
							+ porder.getGmCserver().getCserverName() + "'";
				}
			}
			if (porder.getGmCserver().getSeverType() != null) {
				if (!"".equals(porder.getGmCserver().getSeverType())) {
					queryString = queryString + " and o.gmCserver.severType like '%"
							+ porder.getGmCserver().getSeverType() + "%'";
				}
			}
			if (porder.getGmCserver().getUnit() != null) {
				if (!"".equals(porder.getGmCserver().getUnit())) {
					queryString = queryString + " and o.gmCserver.unit='"
							+ porder.getGmCserver().getUnit() + "'";
				}
			}
			if (porder.getOrderNum()!= null) {
				if (!"".equals(porder.getOrderNum())) {
					queryString = queryString + " and o.orderNum='"
							+ porder.getOrderNum() + "'";
				}
			}
			queryString = queryString + " order by o.createDate desc ";
			System.out.println("findSalerOrderDetial--queryString=" + queryString);
			// getHibernateTemplate().find(queryString);
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public List getNewBuyOrder(String username) {
		try {
			String queryString = "from GmOrder gm where gm.gmUser.userName='"
					+ username + "'and gm.payState=0 and gm.state!=2 and gm.orderType=3";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List getNewSaleOrder(String username) {
		try {
			String queryString = "from GmOrder gm where gm.gmGameStore.gmUser.userName='"
					+ username + "' and (gm.state=0 or gm.state=1) and gm.payState!=2 and gm.orderType=3";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findQuery(Object orderNum) {
		try {
			String queryString = "from GmOrder gm where gm.orderNum=?";
			return getHibernateTemplate().find(queryString, orderNum);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findMoneyCount(GmUser guser) {
		try {
			String queryString = "select sum(gm.factMoney) from GmOrder gm where gm.gmUser=? and gm.state=1 and gm.payState=1";
			return getHibernateTemplate().find(queryString, guser);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			re.printStackTrace();
			throw re;
		}
	}

	public GmOrder merge(GmOrder detachedInstance) {
		log.debug("merging GmOrder instance");
		try {
			GmOrder result = (GmOrder) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmOrder instance) {
		log.debug("attaching dirty GmOrder instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmOrder instance) {
		log.debug("attaching clean GmOrder instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmOrderDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GmOrderDAO) ctx.getBean("GmOrderDAO");
	}
}